package com.srm.psu.glf;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.ScaleAtModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;

public class OptionsActivity extends BaseGameActivity implements IOnMenuItemClickListener{

	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 320;

	protected static final int MENU_MUSIC = 0;
	protected static final int MENU_EFFECTS = MENU_MUSIC + 1;
	
	// ===========================================================
	// Fields
	// ===========================================================

	protected Camera mCamera;

	protected Scene mMainScene;
	protected Handler mHandler;
	
	private BitmapTextureAtlas mBackTexture;

	private Texture mMenuBackTexture;
	private TextureRegion mMenuBackTextureRegion;
	private TextureRegion mBack;

	protected MenuScene mOptionsMenuScene;
	private TextMenuItem mTurnMusicOff, mTurnMusicOn;
	private TextMenuItem mTurnEffectsOff, mTurnEffectsOn;
	private IMenuItem musicMenuItem;
	private IMenuItem effectsMenuItem;

	private Texture mFontTexture;
	private Font mFont;
	
	private Sprite backButton;
	
	//public static boolean isMusicOn = StartActivity.dbhelper.getBgVol();
	//public static boolean isEffectsOn = StartActivity.dbhelper.getFxVol();
	public static boolean isMusicOn = true;
	public static boolean isEffectsOn = true;
	public boolean firstRun = true;
	
	private SharedPreferences audioOptions;
	private SharedPreferences.Editor audioEditor;
	private DbHelper dbhelper;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	
	@Override
	public Engine onLoadEngine() {
		mHandler = new Handler();
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		audioOptions = getSharedPreferences("audio",MODE_PRIVATE);
		audioEditor = audioOptions.edit();
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub
		/* Load Font/Textures. */
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBackTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR);
		this.mFontTexture = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		FontFactory.setAssetBasePath("font/");
		this.mFont = FontFactory.createFromAsset(this.mFontTexture, this,
				"HeeHawRegular.ttf", 32, true, Color.BLACK);
		this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
		this.mEngine.getFontManager().loadFont(this.mFont);
		
		this.mBack=BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackTexture, this, "back.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mBackTexture);
		this.mMenuBackTexture = new BitmapTextureAtlas(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset((BitmapTextureAtlas) this.mMenuBackTexture,
						this, "MainMenu/MainMenuBk.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mMenuBackTexture);
		
		mTurnMusicOn = new TextMenuItem(MENU_MUSIC, mFont, "Turn Music On");
		mTurnMusicOff = new TextMenuItem(MENU_MUSIC, mFont, "Turn Music Off");
		mTurnEffectsOn = new TextMenuItem(MENU_EFFECTS, mFont, "Turn Sound FX On");
		mTurnEffectsOff = new TextMenuItem(MENU_EFFECTS, mFont, "Turn Sound FX Off");
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		this.mOptionsMenuScene = new MenuScene(this.mCamera);
		this.createOptionsMenuScene();

		/* Center the background on the camera. */
		final int centerX = (CAMERA_WIDTH - this.mMenuBackTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mMenuBackTextureRegion.getHeight()) / 2;

		this.mMainScene = new Scene(1);
		/* Add the background and static menu */
		final Sprite menuBack = new Sprite(centerX, centerY, this.mMenuBackTextureRegion);
		mMainScene.getLastChild().attachChild(menuBack);
		mMainScene.setChildScene(mOptionsMenuScene);
		
		backButton = new Sprite(15, 15, this.mBack){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
					final float pTouchAreaLocalY){
				switch(pSceneTouchEvent.getAction()){
					case TouchEvent.ACTION_DOWN:
						break;
					case TouchEvent.ACTION_UP:
						// will later bring up the pause menu
						//Toast.makeText(AutoParallaxBackgroundExample.this, "Pause button pressed!", Toast.LENGTH_SHORT).show();
						OptionsActivity.this.finish();
						Intent myIntent = new Intent(OptionsActivity.this,MainMenuActivity.class);
						OptionsActivity.this.startActivity(myIntent);
						break;
					default:
						break;
				}
				return true;
			}
		};
		mMainScene.registerTouchArea(backButton);
        
		mMainScene.attachChild(backButton);
		

		return this.mMainScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onResumeGame() {
		super.onResumeGame();
		mMainScene.registerEntityModifier(new ScaleAtModifier(0.5f, 0.0f, 1.0f, CAMERA_WIDTH/2, CAMERA_HEIGHT/2));
		mOptionsMenuScene.registerEntityModifier(new ScaleAtModifier(0.5f, 0.0f, 1.0f, CAMERA_WIDTH/2, CAMERA_HEIGHT/2));
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) 
	{
		dbhelper = new DbHelper(this);
		
		// TODO Auto-generated method stub
		switch(pMenuItem.getID()) {
		case MENU_MUSIC:
			if (isMusicOn) {
				isMusicOn = false;
				dbhelper.setBgVol(isMusicOn);
			} else {
				isMusicOn = true;
				dbhelper.setBgVol(isMusicOn);
			}
			mMainScene.clearChildScene();
			createOptionsMenuScene();
			mMainScene.setChildScene(mOptionsMenuScene);				
			return true;
		case MENU_EFFECTS:
			if (isEffectsOn) {
				isEffectsOn = false;
				dbhelper.setFxVol(isEffectsOn);
			} else {
				isEffectsOn = true;
				dbhelper.setFxVol(isEffectsOn);
			}
			mMainScene.clearChildScene();
			createOptionsMenuScene();
			mMainScene.setChildScene(mOptionsMenuScene);	
			return true;
		default:
			return false;
		}
	}
	// ===========================================================
	// Methods
	// ===========================================================
	
	protected void createOptionsMenuScene() {
		this.mOptionsMenuScene.detachChildren();
		this.mOptionsMenuScene = new MenuScene(this.mCamera);
		
		if (isMusicOn) {
			this.musicMenuItem = new ColorMenuItemDecorator( mTurnMusicOff, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f);
		} else {
			this.musicMenuItem = new ColorMenuItemDecorator( mTurnMusicOn, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f);
		}
		this.musicMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mOptionsMenuScene.addMenuItem(this.musicMenuItem);
		
		if (isEffectsOn) {
			this.effectsMenuItem = new ColorMenuItemDecorator( mTurnEffectsOff, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f);
		} else {
			this.effectsMenuItem = new ColorMenuItemDecorator( mTurnEffectsOn, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f);			
		}
		this.effectsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mOptionsMenuScene.addMenuItem(this.effectsMenuItem);
	
		this.mOptionsMenuScene.buildAnimations();
		
		this.mOptionsMenuScene.setBackgroundEnabled(false);

		this.mOptionsMenuScene.setOnMenuItemClickListener(this);
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
